package com.pipayshop.controller;

import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.HotelShopOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实体店订单数据表 前端控制器
 * </p>
 *
 * @author wzx
 * @since 2023-08-01
 */
@Api(value = "实体店用户订单接口（最终版）",tags = "实体店用户订单接口（最终版）")
@RestController
@RequestMapping("/pipayshopapi/shop-order")
@Slf4j
public class HotelShopOrderController {

    @Resource
    private HotelShopOrderInfoService shopOrderInfoService;

    /**
     * 查看未支付/已支付/已完成订单列表接口(通过一个标识id来获取对应的状态的列表展示)
     *  标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单
     */
    @PostMapping("getOrderList")
    @ApiOperation("（买家）用户酒店订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<LiveOrderListVO>> getOrderList(@RequestBody GetLiveOrderDataVO getOrderDataVO) {
        try {
            List<LiveOrderListVO> orderList = shopOrderInfoService.getOrderList(getOrderDataVO);
            return ResponseVO.getSuccessResponseVo(orderList);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("no_order_list_data");
        }
    }
    @PostMapping("getLiveOrderListByShopName")
    @ApiOperation("（买家）用户的全部酒店订单列表展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<LiveOrderListVO>> getLiveOrderListByShopName(@RequestBody GetLiveOrderByNameVO getLiveOrderByNameVO) {
        try {
            List<LiveOrderListVO> orderList = shopOrderInfoService.getLiveOrderListByShopName(getLiveOrderByNameVO);
            return ResponseVO.getSuccessResponseVo(orderList);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("no_order_list_data");
        }
    }
    @GetMapping("getOrderLiveList")
    @ApiOperation("（买家）用户的全部酒店订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<PageDataVO> getOrderLiveList(GetOrderDataVO getOrderDataVO) {
        try {
            PageDataVO orderList = shopOrderInfoService.getOrderLiveList(getOrderDataVO);
            return ResponseVO.getSuccessResponseVo(orderList);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("no_order_list_data");
        }
    }

    /**
     删除订单接口
     */
    @PostMapping("delOrder/{orderId}")
    @ApiOperation("删除酒店订单接口")
    public ResponseVO<String> delOrderByOrderId(@PathVariable String orderId) {
        try {
            int delete = shopOrderInfoService.delOrderByOrderId(orderId);
            if (delete < 1){
                throw new BusinessException("delete_hotel_order_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo("delete_hotel_order_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_hotel_order_failed_contact_staff");
        }
    }

    /**
     订单的详情接口
     */
    @GetMapping("getOrderDetail/{orderId}")
    @ApiOperation("订单（未支付 / 已支付）的详情接口")
    public ResponseVO<ShopOrderDetailVO> getOrderDetail(@PathVariable String orderId) {
        try {
            ShopOrderDetailVO orderDetail = shopOrderInfoService.getOrderDetail(orderId);
            if (orderDetail == null){
                throw new BusinessException("order_detail_retrieval_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo(orderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_detail_retrieval_failed_contact_staff");
        }
    }

    /**
     酒店订单的详情接口
     */
    @GetMapping("getLiveOrderDetail/{orderId}")
    @ApiOperation("酒店订单（未支付 / 已支付）的详情接口")
    public ResponseVO<ShopLiveOrderDetailVO> getLiveOrderDetail(@PathVariable String orderId) {
        try {
            ShopLiveOrderDetailVO orderDetail = shopOrderInfoService.getLiveOrderDetail(orderId);
            if (orderDetail == null){
                throw new BusinessException("order_detail_retrieval_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo(orderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_detail_retrieval_failed_contact_staff");
        }
    }

    /**
     订单改为已完成状态接口
     */
    @PostMapping("completedOrder/{orderId}")
    @ApiOperation("订单已完成接口")
    public ResponseVO<String> completedOrder(@PathVariable String orderId) {
        try {
            int update = shopOrderInfoService.completedOrder(orderId);
            if (update < 1){
                throw new BusinessException("order_confirmation_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo("order_confirmation_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_confirmation_failed_contact_staff");
        }
    }

    /**
     订单超时未支付的失效操作
     */
    @PostMapping("failOrder/{orderId}")
    @ApiOperation("订单超时未支付的失效操作接口")
    public ResponseVO<String> failOrder(@PathVariable String orderId) {
        try {
            int update = shopOrderInfoService.failOrder(orderId);
            if (update < 1){
                throw new BusinessException("order_timeout_cancellation_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo("order_timeout_cancellation_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_timeout_cancellation_failed_contact_staff");
        }
    }
    @PostMapping("changePrice")
    @ApiOperation("未支付订单改价接口")
    public ResponseVO<String> changePrice(String token) {
        try {
            int update = shopOrderInfoService.changePrice(token);
            if (update < 1){
                throw new BusinessException("unpaid_order_price_update_failed_contact_staff");
            }
            return ResponseVO.getSuccessResponseVo("unpaid_order_price_update_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("unpaid_order_price_update_failed_contact_staff");
        }

    }

    /**
     定时轮询删除失效订单接口
     */
    @PostMapping("deleteFailOrders")
    @ApiOperation("定时轮询删除失效订单接口")
    public ResponseVO<String> deleteFailOrders() {
        try {
            shopOrderInfoService.deleteFailOrders();
            return ResponseVO.getSuccessResponseVo("scheduled_polling_delete_invalid_orders_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("scheduled_polling_delete_invalid_orders_failed_contact_staff");
        }
    }


    /**
     * 生成酒店未支付订单
     */
    @PostMapping("generateUnpaidLiveOrder")
    @ApiOperation("生成酒店未支付订单")
    public ResponseVO<String> generateUnpaidLiveOrder(String token) {
        try {
            String orderId = shopOrderInfoService.generateUnpaidLiveOrder(token);
            return ResponseVO.getSuccessResponseVo(orderId);
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("generate_unpaid_hotel_order_failed_contact_staff");
        }
    }

    /**
     支付酒店下单接口
     */
    @PostMapping("payLiveOrder")
    @ApiOperation("支付酒店下单接口")
    public ResponseVO<String> payLiveOrder(String token) {
        try {
            Map<String, String> map = shopOrderInfoService.payLiveOrder(token);
            if (!map.get("flag").equals("true")) {
                throw new BusinessException("hotel_order_payment_failed_contact_staff");
            }
            return new ResponseVO(200,map.get("order_id"),"hotel_order_payment_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("hotel_order_payment_failed_contact_staff");
        }
    }


    @GetMapping("getOrderLiveListByShopId")
    @ApiOperation("(卖家)实体店的酒店全部订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<PageDataVO> getOrderLiveListByShopId(GetOrderDataVO getOrderDataVO) {
        try {
            PageDataVO list = shopOrderInfoService.getOrderLiveListByShopId(getOrderDataVO);
            if (list == null){
                throw new BusinessException("no_order_list_data");
            }
            return ResponseVO.getSuccessResponseVo(list);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("no_order_list_data");
        }
    }

    /**
     * 用户订酒店的时候创建交易订单
     */
    @PostMapping("/hotelTransaction")
    @ApiOperation("酒店支付时创建交易")
    public ResponseVO hotelTransaction(@RequestParam String token){
        try {
            //这里酒店用的，不涉及任何积分转移，因为在下单酒店的时候，支付酒店订单已经做了积分转移了
            int i = shopOrderInfoService.hotelTransaction(token);
            if (i==1){
                return  new ResponseVO<>(200,null,"membership_store_transaction_record_success");
            }
            throw new BusinessException("non_membership_store_no_transaction_record");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("create_transaction_order_failed_contact_staff");
        }

    }

}
